package edu.kit.csl.cratylus.extraction.elki;

import java.util.LinkedList;
import java.util.List;

import de.lmu.ifi.dbs.elki.data.type.SimpleTypeInformation;
import de.lmu.ifi.dbs.elki.datasource.AbstractDatabaseConnection;
import de.lmu.ifi.dbs.elki.datasource.bundle.MultipleObjectsBundle;
import de.lmu.ifi.dbs.elki.datasource.filter.ObjectFilter;
import de.lmu.ifi.dbs.elki.logging.Logging;
import edu.kit.csl.cratylus.datatypes.Candidate;

/**
 * This database contains {@link Candidate} instances in order to cluster them
 * with ELKI. The candidates should be given as argument to the constructor. 
 * This class essentially manages the data flow between cratylus and ELKI by
 * converting {@link Candidate} to {@link ElkiCandidate} instances. 
 */
public class CandidateDatabaseConnection extends AbstractDatabaseConnection {
	
	private static final Logging LOG = Logging.getLogger(
			CandidateDatabaseConnection.class);
	
	private List<Candidate> candidates;

	/**
	 * This constructor is implemented for inheritance reasons. Do not use this
	 * constructor, since it only creates an empty candidate list.
	 *  
	 * @param filters not used
	 */
	protected CandidateDatabaseConnection(List<ObjectFilter> filters) {
		super(filters);
		candidates = new LinkedList<Candidate>();
	}
	
	/**
	 * Main constructor. Initializes the database connection with the set of
	 * candidates.
	 * 
	 * @param candidates candidates for this database connection
	 * @param filters not used
	 */
	public CandidateDatabaseConnection(List<Candidate> candidates, 
			List<ObjectFilter> filters) {
		super(new LinkedList<ObjectFilter>());
		this.candidates = candidates;
	}

	/**
	 * Loads the data from the database connection. This function returns the
	 * {@link Candidate} set given in the constructor as bundle containing
	 * {@link ElkiCandidate}s for further processing with ELKI.
	 * 
	 * @return candidates suitable for ELKI
	 */
	@Override
	public MultipleObjectsBundle loadData() {
		MultipleObjectsBundle bundle = new MultipleObjectsBundle();
		// Translate Candidates to ElkiCandidates
		List<ElkiCandidate> elkiCandidates = new LinkedList<ElkiCandidate>();
		for (Candidate candidate : candidates) {
			elkiCandidates.add(new ElkiCandidate(candidate));
		}
		bundle.appendColumn(
				new SimpleTypeInformation<ElkiCandidate>(ElkiCandidate.class),
				elkiCandidates);
		return bundle;
	}

	/* (non-Javadoc)
	 * @see de.lmu.ifi.dbs.elki.datasource.AbstractDatabaseConnection#getLogger()
	 */
	@Override
	protected Logging getLogger() {
		return LOG;
	}
}
